package com.guojing.jl.nio.bytebuff;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

/**
 * 集中写入： 分散读和集中写可以提高效率，减少数据在ByteBuffer中的拷贝
 */
public class Test7GatheringWrites {
    public static void main(String[] args) {
        ByteBuffer b1 = StandardCharsets.UTF_8.encode("hello");
        ByteBuffer b2 = StandardCharsets.UTF_8.encode("world");
        ByteBuffer b3 = StandardCharsets.UTF_8.encode("你好");

        try  {

            FileChannel channel = new RandomAccessFile("D:\\jl_project\\src\\springboot-learning\\netty-learning\\words2.txt", "rw").getChannel();

            //把三个ByteBuffer一次性写入文件
            channel.write(new ByteBuffer[]{b1, b2, b3});

            
        } catch (IOException e) {
        }
    }
}
